﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics.Contracts;
using Kooboo.Collections;
using System.ComponentModel.Composition;

namespace Kooboo.Commerce.Repositories
{
    [Export(typeof(IPromotionRuleRepository))]
    public partial class EntityRepositories : IPromotionRuleRepository
    {
       
        public IQueryable<IPromotionRule> PromotionRules
        {
            get
            {
                return this.DataReaderContext.PromotionRules;
            }
        }

        public void Post(IPromotionRule promotionRule)
        {
            Contract.Requires(promotionRule != null);

            var item = new PromotionRule()
            {
                EndDate = promotionRule.EndDate,
                IsEnable = promotionRule.IsEnable,
                Name = promotionRule.Name,
                Priority = promotionRule.Priority,
                PromotionName = promotionRule.PromotionName,
                StartDate = promotionRule.StartDate
            };

            this.DataWriterContext.PromotionRules.InsertOnSubmit(item);


            foreach (var i in promotionRule.KillRules)
            {
                var rel = new PromotionExclusiveRule
                {
                    PromotionRule = item,
                    PromotionRuleId2 = i.Id
                };

                this.DataWriterContext.PromotionExclusiveRules.InsertOnSubmit(rel);
            }

            this.DataWriterContext.SubmitChanges();

            promotionRule.Id = item.Id;
        }

        public void Put(IPromotionRule promotionRule)
        {
            Contract.Requires(promotionRule != null);

            var item = this.DataWriterContext.PromotionRules
                .Where(i => i.Id == promotionRule.Id)
                .FirstOrDefault();

            if (item == null)
            {
                throw new KeyNotFoundException("PromotionRule: " + promotionRule.Id);

            }

            item.EndDate = promotionRule.EndDate;
            item.IsEnable = promotionRule.IsEnable;
            item.Name = promotionRule.Name;
            item.Priority = promotionRule.Priority;
            item.PromotionName = promotionRule.PromotionName;
            item.StartDate = promotionRule.StartDate;

            var result = item.KillRules.Compare(promotionRule.KillRules, (i1, i2) => i1.Id == i2.Id, null);

            foreach (var i in result.Added)
            {
                var rel = new PromotionExclusiveRule
                {
                    PromotionRule = item,
                    PromotionRuleId2 = i.Id
                };

                this.DataWriterContext.PromotionExclusiveRules.InsertOnSubmit(rel);
            }

            foreach (var i in result.Deleted)
            {
                var rel = this.DataWriterContext.PromotionExclusiveRules
                    .Where(n => n.PromotionRuleId1 == item.Id && n.PromotionRuleId2 == i.Id)
                    .FirstOrDefault();

                if (rel != null)
                {
                    this.DataWriterContext.PromotionExclusiveRules.DeleteOnSubmit(rel);
                }
            }

            this.DataWriterContext.SubmitChanges();
        }

        public void Delete(IPromotionRule promotionRule)
        {
            if (promotionRule != null)
            {
                var item = this.DataWriterContext.PromotionRules
                .Where(i => i.Id == promotionRule.Id)
                .FirstOrDefault();

                if (item != null)
                {
                    this.DataWriterContext.PromotionExclusiveRules.DeleteAllOnSubmit
                        (
                        this.DataWriterContext.PromotionExclusiveRules
                        .Where(i => i.PromotionRuleId1 == item.Id || i.PromotionRuleId2 == item.Id)
                        );

                    this.DataWriterContext.PromotionRules.DeleteOnSubmit(item);
                    this.DataWriterContext.SubmitChanges();
                }
            }
        }

    }
}
